import {createRouter, createWebHistory} from "vue-router";
import {useUserStore} from "@/store/user.js";

const index = () => import('@/views/index/index.vue')
const write = () => import('@/views/write/write.vue')
const knowledgeBase = () => import('@/views/knowledgeBase/knowledgeBase.vue')
const userProfile = () => import('@/views/userProfile/userProfile.vue')
const search = () => import('@/views/search/search.vue')
const login = () => import('@/views/login/login.vue')
const register = () => import('@/views/register/register.vue')
const knowledgeBaseDetail = () => import('@/views/knowledgeBaseDetail/knowledgeBaseDetail.vue')
const articleDetails = () => import('@/views/articleDetail/articleDetail.vue')
const modifyUserInfo = () => import('@/views/ModifyUserInfo/ModifyUserInfo.vue')
const collect = () => import('@/views/collect/collect.vue')
const myArticle = () => import('@/views/myArticle/myArticle.vue')

const routes = [{
    path: '/',
    name: 'home',
    component: index,
    meta: {
        title: '首页',
        requiresAuth: false
    }
}, {
    path: '/write',
    name: 'write',
    params: true,
    component: write,
    meta: {
        title: '写笔记',
        requiresAuth: true
    }
}, {
    path: '/knowledge-base',
    name: 'knowledge-base',
    component: knowledgeBase,
    meta: {
        title: '知识库',
        requiresAuth: true
    }
}, {
    path: '/userProfile',
    name: 'userProfile',
    component: userProfile,
    meta: {
        title: '个人中心',
        requiresAuth: true
    }
}, {
    path: '/login',
    name: 'login',
    component: login,
    meta: {
        title: '登录',
        requiresAuth: true
    }
}, {
    path: '/register',
    name: 'register',
    component: register,
    meta: {
        title: '注册',
        requiresAuth: false
    }
}, {
    path: '/search',
    name: 'search',
    component: search,
    meta: {
        title: '搜索',
        requiresAuth: true
    }
}, {
    path: '/knowledge-base-detail/:id',
    name: 'knowledge-base-detail',
    component: knowledgeBaseDetail,
    meta: {
        title: '知识库介绍',
        requiresAuth: true
    }
}, {
    path: '/article-detail/:id',
    name: 'article-detail',
    component: articleDetails,
    meta: {
        title: '博客详情',
        requiresAuth: true
    }
},{
    path: '/modify-user-info',
    name: 'ModifyUserInfo',
    component: modifyUserInfo,
    meta: {
        title: '用户信息修改',
        requiresAuth: true
    }
}, {
    path: '/collect',
    name: 'Collect',
    component: collect,
    meta: {
        title: '文章收藏',
        requiresAuth: true
    }
}, {
    path: '/my-article',
    name: 'MyArticle',
    component: myArticle,
    meta: {
        title: "我的文章",
        requiresAuth: true
    }
}]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()

    const requiresAuth = to.meta.requiresAuth
    const isAuthenticated = useUserStore().isAuthenticated

    if (requiresAuth && !isAuthenticated) {
        return next('/login')
    }

    return next()
})



export default router

